package com.team06.dao;

import com.team06.pojo.Depe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class DepeDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(DepeDao.class);
    private final JdbcTemplate jdbcTemplate;

    public DepeDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 向 MSG_DFME_DEPE 表插入数据
     * 修正列名与数据库表结构一致
     */
    public void insertMessage(Depe entity) {
        String sql = "INSERT INTO MSG_DFME_DEPE (" +
                "META_SNDR, META_RCVR, META_SEQN, META_DDTM, META_TYPE, META_STYP, " +
                "DFLT_FLID, DFLT_FATT, DFLT_FFID, DFLT_FIDE, DFLT_STAT, DFLT_ISTA, " +
                "DFLT_MSTA, DFLT_MIST, DFLT_FRTT) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try {
            jdbcTemplate.update(sql,
                    entity.getMetaSnDR(),
                    entity.getMetaRCVR(),
                    entity.getMetaSEQN(),
                    entity.getMetaDDTM(),
                    entity.getMetaTYPE(),
                    entity.getMetaSTYP(),
                    entity.getDfltFLID(),
                    entity.getDfltFATT(),
                    entity.getDfltFFID(),
                    entity.getDfltFIDE(),
                    entity.getDfltSTAT(),
                    entity.getDfltISTA(),
                    entity.getDfltMsta(),
                    entity.getDfltMist(),
                    entity.getDfltFrtt()
            );
            LOGGER.info("成功插入 DEPE 数据到数据库，文件: {}", entity.getXmlFileName());
        } catch (Exception e) {
            LOGGER.error("插入 DEPE 数据失败，异常信息: {}", e.getMessage(), e);
            throw new RuntimeException("插入 DEPE 数据时发生错误", e);
        }
    }
}